'use strict'

define(['jquery', 'bootstrap', 'bootstrapTable', 'bootstrapTableCN', 'select2','shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function() {
        this.initPage();
        this.initCheckbox();
    },
    
    initPage: function(){
        $('select[name="siteId"]').select2({
            minimumResultsForSearch : -1,
            allowClear : false,
            width: '150'
        });

        $('select[name="siteId"]').change(function(){ 

            var siteId = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/satisfaction-survey-create?siteId=" + siteId;

        })
        
        $('select[name="coachId"]').select2({
            minimumResultsForSearch : 5,
            allowClear : true
        });
        
        $("input[name='courseRoundId']").click(function(){
          if($("input[name='courseRoundId']:checked").length > 0) {
        	  $("#send_btn").html("发送问卷（已选" + $("input[name='courseRoundId']:checked").length + "项）")
              $("#send_btn").removeClass("btn-default");
              $("#send_btn").removeClass("btn-shadow-default");
              $("#send_btn").addClass("btn-success");
              $("#send_btn").addClass("btn-shadow-success");
              $("#send_btn").removeAttr("disabled");
          } else {
              $("#send_btn").html("发送问卷")
              $("#send_btn").removeClass("btn-success");
              $("#send_btn").removeClass("btn-shadow-success");
              $("#send_btn").addClass("btn-default");
              $("#send_btn").addClass("btn-shadow-default");
              $("#send_btn").attr("disabled","disabled");
          }
        });
        
        $(document).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        var $form = $(document).find('form[data-validate="true"]')                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
    },
    
    initCheckbox: function(){
        $("input[name='selectAll']").click(function(){
            if ($(this).prop("checked")) {
                $("input[name='courseRoundId']:checkbox").each(function () {
                    $(this).prop("checked", true);
                });
                $("#send_btn").html("发送问卷（已选" + $("input[name='courseRoundId']:checked").length + "项）")
                $("#send_btn").removeClass("btn-default");
                $("#send_btn").removeClass("btn-shadow-default");
                $("#send_btn").addClass("btn-success");
                $("#send_btn").addClass("btn-shadow-success");
                $("#send_btn").removeAttr("disabled");
            } else {
                $("input[name='courseRoundId']:checkbox").each(function () {
                    $(this).prop("checked", false);
                });
                $("#send_btn").html("发送问卷")
                $("#send_btn").removeClass("btn-success");
                $("#send_btn").removeClass("btn-shadow-success");
                $("#send_btn").addClass("btn-default");
                $("#send_btn").addClass("btn-shadow-default");
                $("#send_btn").attr("disabled","disabled");
            }
        })
    }
   }
})
